// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
    env: 'cloud1-0gyq4iqkbbe4572f'
})
const db =cloud.database();


// 云函数入口函数
exports.main = async (event, context) => {
    //声明全局变量，否则db.collection里面改变了无效
    global.nickName='匿名'
    global.headImg=''
    return new Promise((resolve, reject) => {
        db.collection('user').where({
          _openid: event.dynamicOpenid
        }).get().then(res => {
          const { data } = res;
          const nickName = data[0].nickName;
          const headImg = data[0].headImg;
          console.log("测试一下输出的动态的openid",event.desc)
          console.log(event.dynamicOpenid)
          resolve({
            event,
            nickName: nickName,
            headImg: headImg
          });
        }).catch(err => {
            resolve({
                event,
                nickName: nickName,
                headImg: headImg
              });
        });
      });
}